package com.sf.controller.cms;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.sf.kits.easyui.DataGridJson;
import com.sf.model.cms.Article;
import com.sf.model.cms.ArticleType;

import java.util.List;

public class CmsArticleController extends Controller {
    public void index() {
        render("/WEB-INF/static/cms/Article/index.ftl");
    }

    public void list(){
        int page = getParaToInt("page", 1);
        int rows = getParaToInt("rows", 10);
        Page<Record> article_page = Article.me.paginate(rows,page);
        List<Record> list = article_page.getList();
        DataGridJson json = new DataGridJson();
        json.setRows(list);
        json.setTotal(article_page.getTotalRow()+"");
        renderJson(json);
    }


    public void articleIndex() {
        int aid = getParaToInt(0, -1);
        setAttr("aid",aid);
        render("/WEB-INF/static/cms/Article/article.ftl");
    }

    public void edit() {
        String editType = getPara("type", "");
        int aid = getParaToInt("aid", -1);
        if ("".equals(editType.trim()) || null == editType || aid == -1) {
            renderText("出错，需要转404页面");
            return;
        }


        if ("load".equals(editType.trim().toLowerCase())) {
            renderJson(Article.me.findById(aid));
        } else {
            String content = getPara("content");
            boolean flag = Article.me.findById(aid).set("content", content).update();
            renderJson(flag);
        }
    }



    public void type(){
        render("/WEB-INF/static/cms/Article/article_type.ftl");
    }

    public void typeList(){
        int page = getParaToInt("page", 1);
        int rows = getParaToInt("rows", 10);
        Page<Record> article_page = ArticleType.me.paginate(rows,page);
        List<Record> list = article_page.getList();
        DataGridJson json = new DataGridJson();
        json.setRows(list);
        json.setTotal(article_page.getTotalRow()+"");
        renderJson(json);
    }


    public void editType(){
        renderText(getModel(ArticleType.class).update() + "");
    }

    public void addType(){
        renderText(getModel(ArticleType.class).save() + "");
    }

}
